package cool.tianyou.blue.bird.common.util;


import cool.tianyou.blue.bird.common.exception.RestApiException;
import cool.tianyou.blue.bird.common.exception.SecurityAssessException;
import java.util.Collection;

/**
 * 断言工具类
 *
 * @author likeguo
 */
public class Assert {

    private Assert() {
    }



    public static void notNull(Object obj,RuntimeException e) {
        isTrue(obj != null,e);
    }



    /**
     * not null,if null throw RestApiException
     *
     * @param obj     obj
     * @param message exception message
     */
    public static void notNull(Object obj, String message) {
        isTrue(obj != null, message);
    }

    /**
     * if true throw RestApiException
     *
     * @param flag    flag
     * @param message message
     */
    public static void isFalse(boolean flag, String message) {
        isTrue(!flag, message);
    }

    /**
     * if flase throw RestApiException
     *
     * @param flag    flag
     * @param message message
     */
    public static void isTrue(boolean flag, String message) {
        if (!flag) {
            throw new RestApiException(message);
        }
    }


    public static void isTrue(boolean flag, RuntimeException e) {
        if (!flag) {
            throw e;
        }
    }

    /**
     * if true throw SecurityAssessException
     *
     * @param flag    flag
     * @param message message
     */
    public static void isAuth(boolean flag, String message) {
        if (!flag) {
            throw new SecurityAssessException(message);
        }
    }

    /**
     * 判断集合是否为空
     * @param collection 集合
     * @param message 异常提示信息
     */
    public static void isEmpty(Collection<?> collection,String message){
        isTrue(collection==null||collection.isEmpty(),message);
    }
    /**
     * 判断集合是否不为空
     * @param collection 集合
     * @param message 异常提示信息
     */
    public static void notEmpty(Collection<?> collection,String message){
        isTrue(collection!=null&&!collection.isEmpty(),message);
    }


    public static void notEmpty(Collection<?> collection,RuntimeException e){
        isTrue(collection!=null&&!collection.isEmpty(),e);
    }
}
